//
//  SaveTool.m
//  UI19_豆瓣收藏
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SaveTool.h"

@implementation SaveTool
#warning 3. 用来判断文件夹是否存在,如果没有文件夹,就创建一个,如果有不做任何操作.
+ (void)isHaveFolder {
    // 1. 沙盒路径
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 2. 拼接电影的文件夹名
    NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"/Movie"];
    // 创建一个文件的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者调用方法, 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:movieFile]) {
        // 创建文件夹
        [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", movieFile);
    
}

#warning 4.因为按钮上得字显示的是当前model的收藏情况,所以需要先进行反归档,如果存在就显示已收藏,不存在显示收藏
+ (BOOL)isHaveMovieInPlist:(MZYInformationModel *)movie {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    // 反归档
    NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (MZYInformationModel *tempMovie in movieArr) {
        if ([tempMovie.contentid isEqual:movie.contentid]) {
            return NO;
        }
    }
    return YES;
}





#warning 5. 把数据存到对应的文件中
+ (void)saveMovieToPlist:(MZYInformationModel *)model {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    //反归档
    //直接归档会覆盖掉之前的数据,所以先进行反归档,然后添加进去之后在进行归档处理操作
    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (!movieArr) {
        // 如果为空, 先进行初始化
        movieArr = [NSMutableArray array];
    }
    [movieArr addObject:model];
    
    // 归档
    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
    NSLog(@"%@", docPath);
    
}

#warning 6. 取消收藏的方法
+ (void)cancelMovieInPlist:(MZYInformationModel *)movie {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    
    // 反归档
    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历之后移除相应对象
    for (MZYInformationModel *temp in movieArr) {
        if ([temp.contentid isEqual:movie.contentid]) {
            // 移除
            [movieArr removeObject:temp];
            break;
        }
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
}



//
//+ (BOOL)isHaveInFile:(id)object type:(SaveToolType)type {
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    switch (type) {
//        case SaveToolMovie: {
//            // 拼接电影的文件夹路径
//            NSString *docPath = [sandBoxPath stringByAppendingString:@"Movie/movie.plist"];
//            NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//            Movie *movie = object;
//            for (Movie *temp in movieArr) {
//                if ([temp.movieId isEqual:movie.movieId]) {
//                    return NO;
//                }
//            }
//        }
//            break;
//            
//        default:
//            break;
//    }
//    return YES;
//    
//}






@end
